package com.ztjava.core.directive;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.ehcache.CacheKit;
import com.ztjava.core.Constant;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.WrappingTemplateModel;

public class DataDirective implements TemplateDirectiveModel{

	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		try {
			String type = "text";
			if (StrKit.notNull(params.get("type"))) {
				type = params.get("type").toString();
			}
			String code = "";
			if (StrKit.notNull(params.get("code"))) {
				code = params.get("code").toString();
			}
			if(code.equals("")) return;
			String value = "";
			if (StrKit.notNull(params.get("value"))) {
				value = params.get("value").toString();
			}
			String enable_flag = "";
			if(StrKit.notNull(params.get("enable_flag"))){
				enable_flag = params.get("enable_flag").toString();
			}
			Map<String,Record> map = CacheKit.get(Constant.CacheData, code);
			if(map==null){
				return;
			}
			Record record = null;
			String str = "";
			if(type.equals("text")){
				record = map.get(value);
				if(record!=null){
					str = record.getStr("name");
					if(record.get("color")!=null&&!record.getStr("color").equals("")){
						str ="<span style=\"color:"+record.getStr("color")+"\">"+str +"</span>";
					}
				}
				env.getOut().write(str);
		        env.getOut().flush();
			}else if(type.equals("texts")){

				String[] values = value.split(",");
				for(int i=0;i<values.length;i++){
					record = map.get(values[i]);
					
					if(!enable_flag.equals("")){
						if(record.get("enable_flag")==null||!record.getStr("enable_flag").equals(enable_flag)){
							continue;
						}
					}
					
					if(record!=null){
						str = record.getStr("name");
						if(record.get("color")!=null&&!record.getStr("color").equals("")){
							str ="<span style=\"color:"+record.getStr("color")+"\">"+str +"</span>";
						}
						str = str + ",";
					}
				}
				if(!str.equals("")){
					str = str.substring(0,str.length()-1);
				}
		        env.getOut().write(str);
		        env.getOut().flush();
			}else if(type.equals("option")){

				for(String key:map.keySet()){
					record = map.get(key);
					if(!enable_flag.equals("")){
						if(record.get("enable_flag")==null||!record.getStr("enable_flag").equals(enable_flag)){
							continue;
						}
					}
					
					str = str + "<option value=\""+key+"\"";
					if(!value.equals("")&&value.equals(key)){
						str = str + " selected=\"selected\"";
					}
					str = str +">"+record.getStr("name")+"</option>";
				}
		        env.getOut().write(str);
		        env.getOut().flush();
			}else if(type.equals("list")){

				List<Record> list = new ArrayList<Record>();
				for(String key:map.keySet()) {
					record = map.get(key);
					if(!enable_flag.equals("")){
						if(record.get("enable_flag")==null||!record.getStr("enable_flag").equals(enable_flag)){
							continue;
						}
					}
					list.add(map.get(key));
				}
				loopVars[0] = WrappingTemplateModel.getDefaultObjectWrapper().wrap(list);
				body.render(env.getOut());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
